unit UDevIOAdd;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, UDevIOAddEditBase, cxGraphics,
  cxControls, cxLookAndFeels, cxLookAndFeelPainters, cxContainer, cxEdit,
  dxSkinsCore, dxSkinOffice2016Colorful, Vcl.Menus, Data.DB, MemDS, DBAccess,
  Uni, System.ImageList, Vcl.ImgList, cxImageList, Vcl.StdCtrls, cxButtons,
  Vcl.ExtCtrls, cxSpinEdit, cxDropDownEdit, cxMaskEdit, cxTextEdit, cxLabel;

type
  TfrmDevIOAdd = class(TfrmDevIOAddEditBase)
    procedure btnSaveClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmDevIOAdd: TfrmDevIOAdd;

implementation

uses
  UCommonConst, UCommonFunSqlite;

{$R *.dfm}

procedure TfrmDevIOAdd.btnSaveClick(Sender: TObject);
var
  sSql, sIOName, sIP: string;
  iIOID, iPort, iCtrlID: Integer;
begin
  inherited;

  sIOName := Trim(edtName.Text);
  if sIOName.IsEmpty then
  begin
    MsgBoxDefault(RESDEVNAMEEMPTY);
    edtName.SetFocus;
    Abort;
  end;

  if checkExistInDB('DevIOData', 'IOName', sIOName) then
  begin
    MsgBoxDefault(RESDEVNAMEEXIST);
    edtName.SetFocus;
    Abort;
  end;

  sIP := Trim(edtIP.Text);
  if sIP.IsEmpty then
  begin
    MsgBoxDefault(RESDEVIPEMPTY);
    edtIP.SetFocus;
    Abort;
  end;

  if checkExistInDB('DevIOData', 'IOIP', sIP) then
  begin
    MsgBoxDefault(RESDEVIPEXIST);
    edtIP.SetFocus;
    Abort;
  end;


  iPort := edtPort.Value;

  if comDevCtrl.ItemIndex = -1 then
  begin
    MsgBoxDefault(RESDEVCTRLNOSELECTED);
    comDevCtrl.SetFocus;
    Abort;
  end;

 iCtrlID := Integer(comDevCtrl.Properties.Items.Objects[comDevCtrl.ItemIndex]);


  iIOID := getMaxRecordNum('DevIOData', 'IOID') + 1;

  sSql := 'insert into DevIOData (IOID,IOName,IOIP,IOPort,DevCtrlID) values ('
        + QuotedStr(IntToStr(iIOID)) + ','
        + QuotedStr(sIOName) + ','
        + QuotedStr(sIP) + ','
        + QuotedStr(IntToStr(iPort)) + ','
        + QuotedStr(IntToStr(iCtrlID))
        + ')';

  if ExecSql(sSql) then
  begin
    edtName.Clear;
    edtIP.Clear;
    edtName.SetFocus;

    SendMessage(FParentHandle, WM_REFRESHDATA, 0, 0);

    MsgBoxDefault(RESSAVESUCCESS);
  end
  else
    MsgBoxDefault(RESSAVEFAIL);
end;


end.
